package com.aaa.service.impl;

import com.aaa.dao.TeacherListDao;
import com.aaa.entity.EduTeacher;
import com.aaa.service.TeacherListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 李双奇
 * @Date: 2022/12/07/20:46
 * @Description:
 */
@Service
public class TeacherListServiceImpl implements TeacherListService {

    @Autowired
    private TeacherListDao teacherListDao;
    //如果使用mp分页 需要配置分页拦截器
    @Override
    public Result<IPage<EduTeacher>> queryTeacherList(Integer current, Integer pageSize, EduTeacher eduTeacher) {
        IPage<EduTeacher> page = new Page(current, pageSize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        IPage<EduTeacher> iPage = teacherListDao.selectPage(page, wrapper);
        List<EduTeacher> records = iPage.getRecords();
        if (StringUtils.hasText(eduTeacher.getMobile())) {
            String id = eduTeacher.getMobile();
            records = records.stream().filter(index -> index.getMobile().contains(id)).collect(Collectors.toList());
        }
      if(StringUtils.hasText(eduTeacher.getName())){
            String name = eduTeacher.getName();
            records=records.stream().filter(index -> index.getName().contains(name) ).collect(Collectors.toList());
          System.out.println(records.toString());
      }
        if(StringUtils.hasText(eduTeacher.getStatus())){
//            wrapper.like("status",eduTeacher.getStatus());
            String status = eduTeacher.getStatus();
            records=records.stream().filter(index -> index.getStatus().contains(status)).collect(Collectors.toList());
    }
        iPage.setRecords(records);
        return new Result<>(2000,"查询成功",iPage);
    }

    @Override
    public Result updateTeacherList(EduTeacher eduTeacher) {
        UpdateWrapper<EduTeacher> wrapper = new UpdateWrapper<>();
        System.out.println(eduTeacher.getId()+"####################");
        wrapper.eq("id" , eduTeacher.getId());
        wrapper.set("name",eduTeacher.getName());
        wrapper.set("mobile",eduTeacher.getMobile());
        wrapper.set("email",eduTeacher.getEmail());
        wrapper.set("intro",eduTeacher.getIntro());
        teacherListDao.update(null,wrapper);
        return new Result(2000,"修改成功");
    }

    @Override
    public Result changeStatus(EduTeacher eduTeacher) {
        UpdateWrapper<EduTeacher> wrapper = new UpdateWrapper<>();
        wrapper.eq("id" , eduTeacher.getId() );
        wrapper.set("status",eduTeacher.getStatus());
        teacherListDao.update(null,wrapper);

        return new Result(2000,"状态修改成功");
    }
}
